package com.bw.produce.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.db.PageResult;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.produce.domain.*;
import com.bw.produce.domain.dto.ProductDTO;
import com.bw.produce.domain.vo.ProductVO;
import com.bw.produce.mapper.ProductMapper;
import com.bw.produce.query.ProductQueryObject;
import com.bw.produce.service.IProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author lemon
 * @date 2024/11/19 21:03
 **/
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageResult<ProductVO> query(ProductQueryObject productQueryObject) {
        //TODO

        return null;
    }

    @Override
    public void saveOrUpdate(ProductDTO productDTO) {
        /*//优惠
        Promotion promotion = productDTO.getPromotion();
        List<ProductLadder> ladderList = productDTO.getProductLadderList();
        List<ProductFullReduction> fullReductionList = productDTO.getProductFullReductionList();
        List<MemberPrice> memberPriceList = productDTO.getMemberPriceList();

        //图片和sku（必须）
        List<SkuStock> skuStockList = productDTO.getSkuStockList();
        List<ProductImage> imageList = productDTO.getProductImageList();

        //设置展示图
        if (productDTO.getPic() == null) {
            productDTO.setPic(imageList.get(0).getImagePath());
        }

        //拷贝属性
        Product product = new Product();
        BeanUtils.copyProperties(productDTO, product);
        this.saveOrUpdate(product);

        Long id = product.getId();
        //删除图片和sku
        productMapper.removeImageMid(product.getId());
        productMapper.removeSkuMid(product.getId());

        //保存图片和sku
        productMapper.saveImageMid(id, imageList);
        productMapper.saveSkuMid(id, skuStockList);

        //判断有没有优惠
        if (product.getPromotionType() != 0) {
            //维护阶梯价格
            if (CollectionUtil.isNotEmpty(ladderList)) {
                productMapper.removeLadderMid(id);
                productMapper.saveLadderMid(id, ladderList);
            }

            //维护满减优惠
            if (CollectionUtil.isNotEmpty(fullReductionList)) {
                productMapper.removeFullReductionMid(id);
                productMapper.saveFullReductionMid(id, fullReductionList);
            }

            //维护会员价格
            if (CollectionUtil.isNotEmpty(memberPriceList)) {
                productMapper.removeMemberPriceMid(id);
                productMapper.saveLMemberPriceMid(id, memberPriceList);
            }

            //维护促销
            if (promotion != null) {
                productMapper.removePromotion(id);
                productMapper.savePromotion(id, promotion);
            }
        }*/
    }


    @Override
    public void remove(Long id) {
       /* //通过id查询
        ProductVO productVO = productMapper.selectById(id);
        //删除
        productMapper.deleteById(id);
        //删除图片和sku
        productMapper.removeImageMid(id);
        productMapper.removeSkuMid(id);
        //删除优惠
        if (productVO.getPromotionType() != 0){
            productMapper.removeLadderMid(id);
            productMapper.removeFullReductionMid(id);
            productMapper.removeMemberPriceMid(id);
            productMapper.removePromotion(id);
        }*/
    }


}
